﻿cmake_minimum_required(VERSION 3.20)
project(xserial VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

################################## 配置QT5环境 #################################
# 自动调用moc，uic，rcc处理qt的扩展部分
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)



#指定二进制文件位置
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build/${CMAKE_BUILD_TYPE})
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/libs)


# 配置编码方式
add_definitions("-DUNICODE" "-D_UNICODE")
# 设置 UTF-8 编码编译选项
if(MSVC)
    # MSVC 编译器
    add_compile_options(/utf-8)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    # GCC 或 Clang 编译器
    add_compile_options(-finput-charset=UTF-8 -fexec-charset=UTF-8)
else()
    # 其他编译器（需要根据具体编译器文档设置）
    message(WARNING "Unsupported compiler: UTF-8 encoding settings may not be applied.")
endif()



# 查询QT库
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Widgets Gui SerialPort)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Widgets Gui SerialPort)
set(QT_LIBS 
    Qt${QT_VERSION_MAJOR}::Core 
    Qt${QT_VERSION_MAJOR}::Widgets 
    Qt${QT_VERSION_MAJOR}::Gui 
    Qt${QT_VERSION_MAJOR}::SerialPort)


# 添加‘QT_MESSAGELOGCONTEXT’宏，输出日志信息
add_compile_definitions(QT_MESSAGELOGCONTEXT)


include_directories(
    ${PROJECT_SOURCE_DIR}/src
)

# 查询资源文件
file(GLOB_RECURSE CPP_SOURCES ${PROJECT_SOURCE_DIR}/src/*.cpp)
file(GLOB_RECURSE FORM_FILES ${PROJECT_SOURCE_DIR}/src/*.ui)
file(GLOB_RECURSE RC_FILES ${PROJECT_SOURCE_DIR}/src/*.rc)

set(PROJECT_SOURCES ${CPP_SOURCES} ${FORM_FILES} ${RC_FILES})




if(${CMAKE_BUILD_TYPE} STREQUAL "Release")
    if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
        qt_add_executable(${PROJECT_NAME} MANUAL_FINALIZATION WIN32 ${PROJECT_SOURCES})
     else()
        add_executable(${PROJECT_NAME} WIN32 ${PROJECT_SOURCES})
    endif()
else()
    if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
        qt_add_executable(${PROJECT_NAME} MANUAL_FINALIZATION ${PROJECT_SOURCES})
     else()
        add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
    endif()
endif()

    

target_link_libraries(${PROJECT_NAME} PRIVATE ${QT_LIBS})


include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME}
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
# 自动部署
if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(${PROJECT_NAME})
endif()

